package cn.sumpu.app.guoguojie.biz.impl;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sumpu.app.guoguojie.biz.CommonBiz;
import cn.sumpu.app.guoguojie.biz.DoingBiz;
import cn.sumpu.app.guoguojie.biz.FamilyBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.biz.MentionBiz;
import cn.sumpu.app.guoguojie.biz.MessageBiz;
import cn.sumpu.app.guoguojie.constant.OwnerType;
import cn.sumpu.app.guoguojie.domain.Family;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;

public class CommonBizImpl implements CommonBiz {
	@Autowired
	private MessageBiz messageBiz;
	@Autowired
	private DoingBiz doingBiz;
	@Autowired
	private MentionBiz mentionBiz;
	@Autowired
	private FamilyBiz familyBiz;
	@Autowired
	private MemberBiz memberBiz;
	
	
	public String getOwnerByType(String ownerType,HttpServletRequest request)throws AppBusinessException{
		if(!Common.empty(ownerType)){
			if(OwnerType.FAMILY.getValue().equalsIgnoreCase(ownerType)){
				Member member = (Member) request.getSession().getAttribute("member");
				return member.getFamily();
			}
		}
		return null;
	}

	@Override
	public String setOwnerAttribute(String owner, String ownerType,String pageType,
			HttpServletRequest request) throws AppBusinessException {
		Member member = (Member) request.getSession().getAttribute("member");
		if (OwnerType.FAMILY.getValue().equals(ownerType)) {             
			// 判断是否是自己家
			if (member != null && owner.equals(member.getFamily())) {
				// 家左边栏数据
				//int count = messageBiz.messageAllNoReadCount(owner);
				int doingCount = doingBiz.getDoingListCount(owner);
				int mentionCount = mentionBiz.getMentionCount(owner);
				request.setAttribute("mentionCount",Common.getCountFormat(mentionCount));
				request.setAttribute("doingCount", doingCount);
				//request.setAttribute("messageNoRead",Common.getCountFormat(count));
				//家的信息
				Family family = familyBiz.getFamilyByDoor(owner);
				request.setAttribute("family", family);
			} else {
				Family taFamily = familyBiz.getFamilyByDoor(owner);
				request.getSession().setAttribute("taFamily", taFamily);
			
				List<Member> taMemberList = memberBiz.getMemberListByDoorplate(owner);  
				request.getSession().setAttribute("taMemberList", taMemberList);
				request.setAttribute("ownerType", ownerType);
			}

		}
		
		return null;
	}
	
	
	

}
